package com.henau;

import com.henau.dao.ICarMapper;
import com.henau.domain.Car;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.List;

public class test {
    private Reader reader = null;
    private SqlSession sqlSession = null;
    private ICarMapper mapper = null;

    @Before
    public void init() throws IOException {
        reader = Resources.getResourceAsReader("SqlMapConfig.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        sqlSession = factory.openSession();
        mapper = sqlSession.getMapper(ICarMapper.class);
    }

    @After
    public void destroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        reader.close();
    }
    @Test
    public void findAll() throws Exception {
        List<Car> all = mapper.findAll();
        for (Car car:all) {
            System.out.println(car);
        }
    }
    @Test
    public void findByCarID() throws Exception{
        Car car = mapper.findByCarID("豫A99999");
        System.out.println(car);
    }
    @Test
    public void addCar() throws Exception{
        Car car = new Car(3,"豫A99999","黄色","大巴车","宇通快车",60,"未运行","赵文豪",
                "15011111111","123456","河南省","郑州市","二七区","宝马","66666",
                new Date(),"二级","行驶证2",new Date(), "许可证2",new Date(),"型号2");
        int i = mapper.addCar(car);
        System.out.println(i);
    }
    @Test
    public void deleteCar() throws Exception{
        int i = mapper.deleteCar("豫A88888");
        System.out.println(i);
    }
    @Test
    public void updateCar() throws Exception{
        Car id = mapper.findByCarID("豫A88888");
        id.setCarColor("蓝色");
        int i = mapper.updateCar(id);
        System.out.println(i);
    }

}
